package com.javasea.nio.checksum;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

/**
 *@ClassName GetIp
 *@Description TODO
 *@Author longxiaonan@163.com
 *@Date 2021/1/25 0025 15:40
 */
public class GetIp {
    public static void main(String[] args) {
        String localIp = getLocalIp();
        System.out.println(localIp);
    }

    public static String getLocalIp(){
        try{
            Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
            while(en.hasMoreElements()){
                NetworkInterface ni = en.nextElement();
                Enumeration<InetAddress> addresses = ni.getInetAddresses();
                while(addresses.hasMoreElements()){
                    InetAddress address = addresses.nextElement();
                    if (!address.isLoopbackAddress() && !address.isLinkLocalAddress()
                            && address.isSiteLocalAddress()) {
                        return address.getHostAddress() + ":" + address.getHostName();
                    }
                }
            }
        }catch (Exception e) {
        }
        return "127.0.0.1";
    }
}
